home *** CD-ROM | disk | FTP | other *** search
- /*========================================================================*/
- /* UNDO - implements a simple undo facility for the ME Text Editor. */
- /* The undo is fairly simple, but will work for character inserts */
- /* and deletes. Feel free to add more sophisticated processing. */
- /* */
- /* To load : ME -lundo <file> */
- /* To undo a cmd: Press <GREY MINUS> */
- /* */
- /* Kludger - Marc Adler / Magma Systems 3/88 */
- /* */
- /*========================================================================*/
-
- #include mekeys.h
-
- #define MAXUNDO 100 /* The size of our UNDO queue */
- #define NO_CMD -1
-
- int UndoCmd[MAXUNDO]; /* The command which the user pressed */
- int UndoCol[MAXUNDO]; /* The cursor column at the time */
- int UndoLnum[MAXUNDO]; /* The line number at the time */
- string UndoText[MAXUNDO]; /* The text that was changed */
- int UndoIndex; /* The current position in the queue */
-
-
- /* Init() - called once when this macro is loaded into ME */
- init()
- {
- UndoIndex = 0;
- assign_key("new_undo", SH_MINUS);
-
- /* Clear out the undo queue */
- for (i = 1; i <= MAXUNDO; i++)
- UndoCmd[i] = NO_CMD;
-
- /* Event number 8 is trigger just before a command is dispatched */
- add_hook(8, "queue_cmd");
- }
-
-
- /* Queue_Cmd - called just before a keystroke is dispatched */
- queue_cmd()
- {
- /* Increment the queue pointer, and wrap around if at the end */
- UndoIndex++;
- if (UndoIndex > MAXUNDO)
- UndoIndex = 1;
-
- /* Fill the current entry with the key pressed, column, line num */
- UndoCmd[UndoIndex] = cmd = get_lastkey();
- UndoCol[UndoIndex] = currcol();
- UndoLnum[UndoIndex] = currlinenum();
- UndoText[UndoIndex] = "";
-
- /* If a self-insert key was pressed, record the char under the cursor */
- if (cmd >= ' ' && cmd < 127)
- {
- UndoText[UndoIndex] = chr(currchar());
- /* A trick! A negative typeable char means that the char was overstruck */
- if (!get_option("in"))
- UndoCmd[UndoIndex] = -cmd;
- return;
- }
-
- /* Special processing */
- switch (cmd)
- {
- case _DELCHAR :
- UndoText[UndoIndex] = chr(currchar());
- break;
-
- case BACKSPACE :
- UndoText[UndoIndex] = substr(currline(), currcol() - 1, 1);
- break;
-
- case SH_FIVE : /* DelEOL */
- UndoText[UndoIndex] = substr(currline(), currcol(), 256);
- break;
-
- case SH_DEL : /* DelWord */
- save_position();
- restore_position();
- break;
-
- case F1 : /* DelLine */
- UndoText[UndoIndex] = currline();
- break;
-
- default :
- break;
- }
- }
-
-
- new_undo()
- {
- if (UndoIndex == 0 || UndoCmd[UndoIndex] == -1)
- {
- message("No commands to undo");
- return;
- }
-
- cmd = UndoCmd[UndoIndex];
-
- if ((cmd >= ' ' && cmd < 127) || cmd < 0)
- {
- backspace();
- if (cmd < 0) /* overstruck text */
- {
- insert(UndoText[UndoIndex]);
- left();
- }
- goto bye;
- }
-
- if (is_cursor_motion(cmd))
- {
- restore_cursor();
- goto bye;
- }
-
- switch (cmd)
- {
- case _DELCHAR :
- insert(UndoText[UndoIndex]);
- left();
- break;
-
- case BACKSPACE :
- insert(UndoText[UndoIndex]);
- break;
-
- case SH_FIVE : /* DelEOL */
- insert(UndoText[UndoIndex]);
- restore_cursor();
- break;
-
- case SH_DEL : /* DelWord */
- insert(UndoText[UndoIndex]);
- restore_cursor();
- break;
-
- case F1 : /* DelLine */
- insert(UndoText[UndoIndex]);
- restore_cursor();
- break;
-
- case F2 : /* InsLine */
- restore_cursor();
- delline();
- break;
-
- case SH_F2 : /* AppndLine */
- restore_cursor();
- delline();
- break;
-
- default :
- break;
- }
-
- bye:
- message("Command Undone");
-
- /* Dequeue the command */
- UndoCmd[UndoIndex] = -1;
- UndoIndex--;
- if (UndoIndex <= 0)
- UndoIndex = MAXUNDO;
- }
-
- /*-------------------------------------------------------------------------*/
-
- /* is_cursor_motion - returns TRUE if the cmd was a command that simply */
- /* moved the cursor position. */
- is_cursor_motion(cmd)
- int cmd;
- {
- if (cmd == _UP || cmd == _DOWN || cmd == _LEFT || cmd == _RIGHT ||
- cmd == HOME || cmd == END || cmd == CTRL_HOME || cmd == CTRL_END ||
- cmd == PGUP || cmd == PGDN || cmd == CTRL_PGUP || cmd == CTRL_PGDN ||
- cmd == SH_PGDN || cmd == SH_PGUP ||
- cmd == CTRL_LEFT || cmd == CTRL_RIGHT ||
- cmd == F5 || cmd == SH_F5 || cmd == ALT_G || cmd == CTRL_B)
- return 1;
-
- return 0;
- }
-
- restore_cursor()
- {
- goline(UndoLnum[UndoIndex]);
- setcol(UndoCol[UndoIndex]);
- }
-